layui.define(['jquery', 'form'], function(exports) {
    var $ = layui.jquery;
    var form = layui.form;
    var authtree = {
        // 渲染 tree
        render: function(dst, data, opt) {
            var inputname = opt.inputname ? opt.inputname : 'menuIds[]';
            var layfilter = opt.layfilter ? opt.layfilter : 'checkauth';
            var openall = opt.openall ? opt.openall : false;
            var autowidth = opt.autowidth ? opt.autowidth : true;
            var checkedIds = opt.checkedIds ? opt.checkedIds : [];

            $(dst).html(this.renderAuth(data, inputname, layfilter, checkedIds));
            form.render('checkbox');

            // 如果开启自动宽度优化
            if (autowidth) {
                $(dst).css({
                    'whiteSpace': 'nowrap',
                    'maxWidth' : '100%',
                });
                $(dst).find('.layui-form-checkbox').css({
                    'margin': '0',
                });
            }

            if (openall) {
                $(dst).find('.auth-single:first').siblings('.auth-child').show();
            }
            
            this.bindEvents(dst);
            
            return this;
        },
        // 递归渲染子节点
        renderAuth: function(data, inputname, layfilter, checkedIds) {
            var html = '<div class="auth-single">';
            
            // 参数配置
            var childrenKey = 'children';
            var nameKey = 'title';
            var valueKey = 'id';

            // 是否有子节点
            var hasChild = data[childrenKey] && data[childrenKey].length > 0;
            
            // 表示当前节点是否选中
            var isChecked = checkedIds.indexOf(data[valueKey]) !== -1;
            
            html += '<input type="checkbox" name="' + inputname + '" title="' + data[nameKey] + '" value="' + data[valueKey] + '" lay-skin="primary" lay-filter="' + layfilter + '" ' + (isChecked ? 'checked="checked"' : '') + ' ' + (hasChild ? 'data-parent="1"' : '') + '>';

            if (hasChild) {
                html += '<div class="auth-child" style="display:none;">';
                for (var i = 0; i < data[childrenKey].length; i++) {
                    html += this.renderAuth(data[childrenKey][i], inputname, layfilter, checkedIds);
                }
                html += '</div>';
            }
            html += '</div>';

            return html;
        },
        // 绑定事件
        bindEvents: function(dst) {
            var _this = this;
            // 点击展开/收起
            $(dst).find('.auth-single:first').on('click', '[data-parent]', function() {
                var $child = $(this).siblings('.auth-child');
                if ($child.is(':hidden')) {
                    $child.show();
                } else {
                    $child.hide();
                }
            });
        },
        // 获取选中的叶子节点ID
        getChecked: function(dst) {
            var ids = [];
            $(dst).find('input[type="checkbox"]:checked').each(function() {
                ids.push(parseInt($(this).val()));
            });
            return ids;
        }
    };

    exports('authtree', authtree);
}); 